package com.meeruu.sg.account.core.domain.biz;

import com.meeruu.boot.ucs.annotation.NamedCache;
import com.meeruu.boot.ucs.datasource.redis.IRedisCache;
import com.meeruu.common.utils.LoggerUtil;
import com.meeruu.sg.account.core.api.constant.AccountGroup;
import com.meeruu.sg.account.core.api.constant.AccountType;
import com.meeruu.sg.account.core.api.dto.response.UserAccountResponseDTO;
import com.meeruu.sg.account.core.domain.mapper.UserAccountMapper;
import com.meeruu.sg.account.core.domain.model.UserAccountDO;
import com.meeruu.sg.account.core.domain.model.cache.CacheKeys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Auther: yangqintao
 * @Date: 2019/7/13 11:57
 * @Description:
 */
@Slf4j
@Service
public class AccountCacheBiz {

    @NamedCache("default")
    private IRedisCache redisCache;

    @Autowired
    private AccountQueryBiz accountQueryBiz;

    @Autowired
    private UserAccountMapper userAccountMapper;


    public UserAccountDO getUserAccountCache(String userCode, AccountType accountType) {
        UserAccountDO userAccount = redisCache.hget(CacheKeys.USER_ACCOUNT_KEY, userCode + ":" + accountType.getCode(), UserAccountDO.class);
        if (null == userAccount) {
            userAccount = userAccountMapper.selectByUserCodeAndType(userCode, accountType.getCode());
            if (null != userAccount) {
                redisCache.hset(CacheKeys.USER_ACCOUNT_KEY, userCode + ":" + accountType.getCode(), userAccount);
            }
        }

        return userAccount;
    }


    public void cleanCache(String accountId, AccountGroup accountGroup, AccountType accountType) {
        switch (accountGroup) {
            case ORG:
                break;
            case USER:
                this.cleanUserAccountCache(accountId, accountType);
                break;
            case MERCHANT:
                break;
        }
    }


    public void cleanUserCache(String userCode) {

        for (AccountType accountType : AccountType.values()) {
            redisCache.hdel(CacheKeys.USER_ACCOUNT_KEY, userCode + ":" + accountType.getCode());
            LoggerUtil.info(log, "clean userAccount cache success, userCode={}, accountType={}", userCode, accountType);
        }
    }

    private void cleanUserAccountCache(String accountId, AccountType accountType) {
        String userCode = redisCache.hget(CacheKeys.ACCOUNT_ID_CODE_MAP_KEY, accountId, String.class);
        if (null == userCode) {
            UserAccountResponseDTO accountDB = (UserAccountResponseDTO) accountQueryBiz.getAccount(accountId, AccountGroup.USER);
            if (null == accountDB) {
                LoggerUtil.error(log, "用户账户信息不存在, accountId={}", accountId);
                return;
            }
            userCode = accountDB.getUserCode();
            redisCache.hset(CacheKeys.ACCOUNT_ID_CODE_MAP_KEY, accountId, userCode);
        }

        redisCache.hdel(CacheKeys.USER_ACCOUNT_KEY, userCode + ":" + accountType.getCode());
        LoggerUtil.info(log, "clean userAccount cache success, userCode={}, accountType={}", userCode, accountType);
    }
}
